package com.woniu.defecttracking.aop;

import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Aspect
@Component
public class NoAdd {
    @Around("@annotation(com.woniu.defecttracking.annotation.AddNum)")
    public Object addNo(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        //获取被注解的方法
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        //获取方法上的注解内容
        AddNum annotation = method.getDeclaredAnnotation(AddNum.class);
        String NumName = annotation.value();

        for (Object arg : args) {
            Class<?> aClass = arg.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (declaredField.getName().equals(NumName)) {
                    declaredField.setAccessible(true);
                    declaredField.set(arg,NumName.substring(0,3)+ComUtil.emailCode(8));
                    declaredField.setAccessible(false);
                }
            }
        }
        return joinPoint.proceed(args);


    }

}
